from django.db import models

__all__ = ['User', 'Address', 'Product', 'ProductDetail', 'ProductAttribute', 'Order', 'OrderProduct', 'Shopcarts', 'Token',
           'Coupon', 'CouponUse']


# 用户实体类
class User(models.Model):
    user_uuid = models.CharField(max_length=64, verbose_name='uuid', unique=True)
    username = models.CharField(max_length=32, null=True, verbose_name='用户名')
    user_js_code = models.CharField(max_length=64, verbose_name='用户js_code')
    user_point = models.IntegerField(default=0, verbose_name='用户积分')  # 用户积分
    leve_type = ((1, '普通会员'), (2, '白银会员'), (3, '黄金会员'), (4, '钻石会员'))
    user_level = models.IntegerField(choices=leve_type, default=1, verbose_name='用户等级')  # 用户会员级别
    user_money = models.FloatField(default=0, verbose_name='用户余额')
    login_time = models.DateTimeField(auto_now=True, verbose_name='登录时间')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='用户创建时间', null=True)
    is_active = models.BooleanField(default=True, verbose_name='是否激活')

    class Meta:
        verbose_name = '01-用户表'
        verbose_name_plural = verbose_name
    #     managed = False
    #     db_table = 'wechat_user'

    def __str__(self):
        return self.user_uuid


# 地址实体表
class Address(models.Model):
    addr_name = models.CharField(max_length=32, verbose_name='收件人')
    addr_phone = models.CharField(max_length=32, verbose_name='联系方式')
    addr_zip = models.CharField(max_length=32, null=True, verbose_name='邮编')
    addr_province = models.CharField(max_length=34, verbose_name='省份')
    addr_city = models.CharField(max_length=32, verbose_name='城市')
    addr_district = models.CharField(max_length=64, verbose_name='具体地址')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='关联用户')
    is_default = models.BooleanField(default=False, verbose_name='是否默认地址')
    is_active = models.BooleanField(default=True, verbose_name='是否激活地址')

    def __str__(self):
        return '{}---{}'.format(self.addr_name, self.addr_district)

    class Meta:
        verbose_name = '02-地址表'
        verbose_name_plural = verbose_name
        ordering = ['id']
        # 联合约束
        # unique_together = ()
    #     managed = False
    #     db_table = 'wechat_address'


# 商品信息实体类
class Product(models.Model):
    product_id = models.CharField(max_length=64, unique=True, verbose_name='商品编号')
    product_name = models.CharField(max_length=64, verbose_name='商品名称')
    product_indate = models.DateTimeField(auto_now_add=True, verbose_name='商品入库时间')  # 商品入库时间
    product_type_status = ((1, '类型1'), (2, '类型2'), (3, '类型3'))
    product_type = models.IntegerField(choices=product_type_status, verbose_name='商品类型')    # 商品类型
    show_img = models.TextField(verbose_name='商品展示图')   # 商品展示图片
    descript = models.TextField(verbose_name='商品描述')   # 商品描述
    publish_status = models.BooleanField(default=True, verbose_name='是否上架')  # 商品状态

    class Meta:
        verbose_name = '03-商品信息主表'
        verbose_name_plural = verbose_name
        # managed = False
        # db_table = 'wechat_product'

    def __str__(self):
        return self.product_name


class ProductDetail(models.Model):
    product_swiper_img = models.TextField(verbose_name='商品轮播图')
    product_title = models.CharField(max_length=64, verbose_name='商品标题')
    product_title_description = models.CharField(max_length=128, null=True, verbose_name='商品简单描述')
    product_detail = models.TextField(null=True, verbose_name='商品详情描述')
    product_detail_img = models.TextField(null=True, verbose_name='商品详情图片')
    product = models.OneToOneField(to=Product, on_delete=False, verbose_name='关联商品')

    class Meta:
        verbose_name = '04-商品详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.product_title


# 商品属性表
class ProductAttribute(models.Model):
    attribute = models.CharField(max_length=32, verbose_name='商品属性')     # 商品属性
    characteristic = models.CharField(max_length=32, verbose_name='商品规格')    # 商品规格（颜色，大小）
    attribute_img = models.URLField(null=True, verbose_name='展示图')
    stock_quantity = models.IntegerField(default=0, verbose_name='库存数量')     # 库存数量
    price = models.CharField(max_length=64, verbose_name='价格')
    # 主键引用，设置外键关联
    productDetail = models.ForeignKey(to=ProductDetail, on_delete=models.CASCADE, verbose_name='关联商品')

    class Meta:
        verbose_name = '05-商品属性表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.productDetail, self.characteristic, self.attribute, '  ', self.stock_quantity


class Order(models.Model):
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='用户')
    order_numbering = models.CharField(max_length=32, verbose_name='订单编号')
    order_price = models.FloatField(verbose_name='订单支付金额')
    order_status_type = ((1, '已支付'), (2, '未支付'), (3, '异常订单'), (4, '已取消的订单'), (5, '已完成订单'), (6, '已发货'))
    order_status = models.IntegerField(choices=order_status_type, verbose_name='订单状态')
    address = models.ForeignKey(to=Address, on_delete=False, verbose_name='关联地址')
    district_money = models.CharField(max_length=32, null=True, verbose_name='优惠金额')      # 优惠金额
    freight = models.FloatField(null=True, verbose_name='运费')   # 运费
    logistics_name = models.CharField(max_length=32, null=True, verbose_name='物流公司')    # 物流公司
    logistics_num = models.CharField(max_length=64, null=True, verbose_name='物流单号')     # 物流单号
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='订单创建时间')    # 订单创建时间
    receive_time = models.DateTimeField(null=True, verbose_name='订单完成时间')   # 收货时间
    pay_time = models.DateTimeField(null=True, verbose_name='支付时间')   # 支付时间

    class Meta:
        verbose_name = '06-订单表'
        verbose_name_plural = verbose_name
    #     managed = False
    #     db_table = 'wechat_order'


class OrderProduct(models.Model):
    product = models.ForeignKey(to=Product, on_delete=False, verbose_name='关联商品')
    product_attribute = models.ForeignKey(to=ProductAttribute, on_delete=False, verbose_name='关联商品属性')
    product_number = models.IntegerField(verbose_name='商品数量')
    order = models.ForeignKey(to=Order, on_delete=models.CASCADE, verbose_name='关联订单')

    def __str__(self):
        return self.product

    class Meta:
        verbose_name = '06-1 -订单商品列表'
        verbose_name_plural = verbose_name


class Shopcarts(models.Model):
    user = models.ForeignKey(User, on_delete=False, verbose_name='关联用户')
    product = models.ForeignKey(to=Product, on_delete=False, verbose_name='关联商品')
    product_attribute = models.OneToOneField(to=ProductAttribute, on_delete=False, verbose_name='关联商品属性')
    product_number = models.IntegerField(verbose_name='商品数量')

    def __str__(self):
        return self.product, self.product, self.product_number

    class Meta:
        verbose_name = '07-购物车'
        verbose_name_plural = verbose_name


class Token(models.Model):
    token = models.UUIDField(verbose_name='token')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    user = models.OneToOneField(to=User, on_delete=False, verbose_name='关联用户')

    class Meta:
        verbose_name = '08-token'
        verbose_name_plural = verbose_name


class Coupon(models.Model):
    coupon_name = models.CharField(max_length=64, verbose_name='优惠券标题')
    coupon_type_choices = ((1, '满减优惠券'), (2, '通用优惠券'))
    coupon_type = models.IntegerField(choices=coupon_type_choices, verbose_name='优惠券类型')
    amount = models.CharField(max_length=32, verbose_name='等值金额')
    coupon_number = models.PositiveIntegerField(verbose_name='数量')
    min_amount = models.CharField(max_length=32, default=0, verbose_name='最低消费金额', help_text='满减劵时填写')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='优惠券创建时间')
    receive_time = models.DateTimeField(verbose_name='优惠券开始发放时间')
    effective_time = models.DateTimeField(verbose_name='优惠券生效时间')
    invalid_time = models.DateTimeField(verbose_name='优惠券失效时间')

    def __str__(self):
        return '{}-{}'.format(self.coupon_name, self.coupon_type)

    class Meta:
        verbose_name = '09-优惠券'
        verbose_name_plural = verbose_name


class CouponUse(models.Model):
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='绑定用户')
    coupon = models.ForeignKey(to=Coupon, on_delete=models.CASCADE, verbose_name='优惠券')
    status_choices = ((1, '未使用'), (2, '已使用'), (3, '已过期'))
    status = models.IntegerField(choices=status_choices, verbose_name='用户优惠券状态')
    obtain_time = models.DateTimeField(verbose_name='领取时间')
    coupon_use_time = models.DateTimeField(null=True, verbose_name='使用时间')

    def __str__(self):
        return '{}-{}'.format(self.user, self.coupon)

    class Meta:
        verbose_name = '10-优惠券使用记录'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'coupon')    # 联合约束，用户与优惠券类型
